package com.example.administrator.myjdlc.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * Created by Administrator on 2017/4/16.
 */

public class DBInitHelper extends SQLiteOpenHelper {
    private static int dbVersion = 1;
    private static String dbName = "";
    private static String tableName = "";
    private static String createTableSql = "";
    private static String upgradeSql = "";
    private static String downgradeSql = "";

    public static void setDbVersion(int dbVersion) {
        DBInitHelper.dbVersion = dbVersion;
    }

    public static void setDbName(String dbName) {
        DBInitHelper.dbName = dbName;
    }

    public static void setTableName(String tableName) {
        DBInitHelper.tableName = tableName;
    }

    public static void setCreateTableSql(String createTableSql) {
        DBInitHelper.createTableSql = createTableSql;
    }

    public DBInitHelper(Context context) {
        super(context, dbName, (SQLiteDatabase.CursorFactory)null, dbVersion);
    }

    public void onCreate(SQLiteDatabase db) {
        db.execSQL(createTableSql);
    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if(upgradeSql == "") {
            db.execSQL("DROP TABLE IF EXISTS " + tableName);
            this.onCreate(db);
        } else {
            db.execSQL(upgradeSql);
        }

    }

    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.i("xinye", "#############数据库降级了##############：" + dbVersion);
        super.onDowngrade(db, oldVersion, newVersion);
        db.execSQL(downgradeSql);
    }
}
